#ifndef BM_UTIL_H
#define BM_UTIL_H

#define DEFINE_VECTOR(type, name)\
	typedef struct				\
	{							\
		int count;				\
		int max_size;			\
		type * elements;		\
	}name;						\
	

#define VEC_SAFE(vector)(vector.elements != NULL && vector.max_size > 0 && vector.count > -1)

#define VEC_INIT(type, size, vector){						\
	vector.elements = (type*)malloc(sizeof(type) * size);	\
	vector.count = 0;										\
	vector.max_size = size;}

#define VEC_FREE(type, vector)		\
	if(vector.elements != NULL){	\
		free(vector.elements);		\
		vector.elements = NULL;		\
		vector.count = 0;			\
		vector.max_size = 0;		\
	}

#define VEC_PUSH(type, vector, item){																\
	if(VEC_SAFE(vector)){																			\
		if(vector.count > vector.max_size - 1)														\
		{																							\
			vector.max_size *= 2;																	\
			vector.elements = (type*)realloc(vector.elements, sizeof(type) * vector.max_size);		\
		}																							\
		vector.elements[vector.count] = item;														\
		++vector.count;																				\
	}}

#define VEC_POP(vector, item){													\
	if(VEC_SAFE(vector)){														\
		if(vec->count > 0)--vec->count;											\
	}}


#define VEC_REMOVE(type, vector, item){																			\
	int i;																										\
	if(VEC_SAFE(vector))																						\
	{																											\
		for(i = 0; i < vector.count; i++)																		\
		{																										\
			if(vector.elements[i] == item)																		\
			{																									\
				memmove(vector.elements+i, vector.elements+i+1, sizeof(type) * (vector.count - i - 1));			\
				--vector.count;																					\
			}																									\
		}																										\
	}}




#endif